import { Component, Input, OnInit, Inject, forwardRef } from '@angular/core';
import { ComItem } from '../../route/entry/com-item';
import { EventService } from '../../shared/service/event.service';
import { HttpService } from '../../shared/service/http.service';
@Component({
    selector: 'app-top-tab',
    templateUrl: './top-tab.component.html',
    styleUrls: ['./top-tab.component.scss']
})
export class TopTabComponent implements OnInit {
    subComs: ComItem[] = [];
    @Input() tagData: any;
    aWidth: string;
    isSelected: boolean;
    currentClickObj: any;
    currentCLickIndexTab: any;
    isRootURL: any; // 用于检查URL是否有变化
    receiverTag = 'TopTabComponentReceiver';
    constructor(
        private eventService: EventService,
        private httpService: HttpService,
    ) {
        if (this.eventService.eventEmit.listenerCount('topTapPart1') > 0) {
            this.eventService.eventEmit.removeListener('topTapPart1');
        }
        this.eventService.eventEmit.on("topTapPart1", res => {
            console.log('eventEmit.on topTapPart1');
            if (localStorage.getItem('topTapPart1')) {
                const parseData = JSON.parse(localStorage.getItem('topTapPart1'));
                this.tagData.currentData = parseData;
            }
        });
        if (this.eventService.eventEmit.listenerCount('topTapPart2') > 0) {
            this.eventService.eventEmit.removeListener('topTapPart2');
        }
        this.eventService.eventEmit.on("topTapPart2", res => {
            console.log('eventEmit.on topTapPart2');
            if (localStorage.getItem('topTapPart2')) {
                const parseData = JSON.parse(localStorage.getItem('topTapPart2'));
                this.tagData.currentData = parseData;
            }
        });

        if (this.eventService.eventEmit.listenerCount(this.receiverTag) > 0) {
            this.eventService.eventEmit.removeListener(this.receiverTag);
        }
        this.eventService.eventEmit.on(this.receiverTag, res => {
            if (localStorage.getItem(this.receiverTag + 'Tag')) {
                localStorage.removeItem(this.receiverTag + 'Tag');
                this.subComs.push(res);
            }
        });
    }

    ngOnInit() {
        const cDataObj = this.tagData.currentData;
        this.reloadCurrentTab();
        if (cDataObj !== undefined) {
            const tags = cDataObj.tags;
            if (tags !== undefined && tags.length > 0) {
                const len = tags.length;
                for (let i = 0; i < len; i++) {
                    // console.log('msg----->', tags[i]);
                    const tag = tags[i];
                    if (tag.linearLayout === 'body') {
                        // this.tabService.getComsDataByStyle(tag.style, callData => {
                        //     if (callData) {
                        //         this.subComs.push(callData);
                        //     }
                        // }, tag);
                        // 发送事件
                        // #task-view-916
                        if (this.tagData.refresh) {
                            tag.isPullToRefresh = this.tagData.refresh;
                        }
                        const eventEmitObj = { style: tag.style, tag, receiverTag: this.receiverTag };
                        localStorage.setItem('generatedComItemTag', JSON.stringify(eventEmitObj));
                        this.eventService.eventEmit.emit('generatedComItem', '');
                    } else {
                        localStorage.setItem("refreshFooterComsTag", JSON.stringify(tag));
                        this.eventService.eventEmit.emit("refreshFooterComsContainer", "refreshFooterComsContainer");
                    }
                }
            }
        }
    }

    change(obj, indexTab) {
        if (obj !== undefined && obj.partLink !== undefined) {
            this.tagData.currentTab = indexTab;
            if (localStorage.getItem('topTapPart' + indexTab)) {
                const parseData = JSON.parse(localStorage.getItem('topTapPart' + indexTab));
                this.tagData.currentData = parseData;
            } else {
                this.httpService.post('application/part',
                    {
                        viewId: obj.partLink.target,
                        sessionId: localStorage.getItem("sessionId"),
                        params: obj.partLink.params
                    }, (res: any) => {
                        if (res !== undefined && res != null) {
                            setTimeout(() => {
                                this.tagData.currentData = JSON.parse(res.script);
                                localStorage.setItem("dataMapTopTabContainer", JSON.stringify(this.tagData));
                                this.eventService.eventEmit.emit('refreshTopTabContainer', 'refreshTopTabContainer');
                            }, 50);

                        }
                    });
            }
        }
    }
    reloadCurrentTab() {
        // 判断是否已经注册事件过了如果已经注册过了 就不用再注册了
        if (this.eventService.eventEmit.listenerCount('refreshSubTab') > 0) {
            this.eventService.eventEmit.removeListener('refreshSubTab');
        }
        this.eventService.eventEmit.on('refreshSubTab', data => {
            console.log('refreshSubTab------->home');
            if (this.tagData.currentTab) {
                document.getElementById('homeNavTab' + this.tagData.currentTab).click();
            }
        });
    }
}
